﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using MapLibrary.Players.Abilities.MapLibrary.Players.Skills;
using Animation;

namespace MapLibrary.Players.Skills
{
    class HookShot : Ability
    {
               /// <summary>
        /// remaining frames until skill can be activated
        /// </summary>
        private int cooldown;

        /// <summary>
        /// amount of frames to wait after last shot
        /// </summary>
        private int reuseTime;

        /// <summary>
        /// velocity to fire the net
        /// </summary>
        private float velocity;

        private int lifetime;

        public bool Active { get; set; }

        private Hook currentHook;
        private Player player;
        public bool HasAttached { get; set; }

        public HookShot() : base()
        {
            reuseTime = 100;
            velocity = 10.0f;
            Active = false;
            HasAttached = false;
            lifetime = 300;
        }

        public override bool ActivateConditionsSatisfied(Player p)
        {
            return cooldown == 0 || Active;
        }

        public void Deactivate()
        {
            Active = false;
            currentHook = null;
            HasAttached = false;
            if (player != null)
            {
                player.Body.IgnoreGravity = false;
            }
        }

        public void Cancel()
        {
            if (currentHook != null)
            {
                currentHook.Remove();
            }
            Deactivate();
        }

        public override void Activate(Player p)
        {
            if (!Active)
            {
                player = p;
                cooldown = reuseTime;
                Active = true;
                currentHook = p.World.ProjectileManager.AddHook(p, velocity) as Hook;
                currentHook.hookShot = this;
                currentHook.TimeToLive = lifetime;
            }
            else
            {
                Cancel();
            }
        }

        public override void Update()
        {
            cooldown--;
            if (cooldown < 0)
            {
                cooldown = 0;
            }
            if (HasAttached && currentHook != null)
            {
                if (currentHook.AttachedToWall)
                {
                    player.Body.IgnoreGravity = true;
                    Vector2 dir = currentHook.GetPosition() - player.GetPosition();
                    dir.Normalize();
                    dir *= 16.0f;
                    //player.Body.ApplyLinearImpulse(dir);
                    player.Body.LinearVelocity = dir;
                }
                else
                {
                   /* Sprite s = currentHook.Joint.BodyB.UserData as Sprite;
                    Vector2 dir = player.GetPosition() - s.GetPosition();
                    dir.Normalize();
                    //dir *= 2.0f;
                    //s.Body.ApplyLinearImpulse(dir);*/
                }
            }
            else if(player != null)
            {
                player.Body.IgnoreGravity = false;
            }
        }
    }
}
